package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author lifeihu
 * @date 2021/12/23
 * @TIME:18:50
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token){
        //1. 校验token
       /* boolean flag = JwtUtils.verifyToken(token);
        if(!flag){
            return ResponseEntity.status(401).body(null);
        }*/
        //2. 获取token中的用户信息
       /* Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        Long id = UserHolder.get().getId();
        if (userID == null) {
            userID = id;
        }
        UserInfoVo userInfoVo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfoVo);
    }
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //1、判断token是否合法
       /* boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }*/
        //2、获取id
       /* Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");*/
        Long id = UserHolder.get().getId();
        //设置id
        userInfo.setId(id);
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }
}
